﻿using System;
using System.Windows;
using System.Windows.Data;
using theeg.GraphWpf.Models;

namespace theeg.GraphWpf.Views
{
    public class NodeShapeToTemplateConverter : DependencyObject, IValueConverter
    {
        #region templates
        public DataTemplate RectangleTemplate
        {
            get { return (DataTemplate)GetValue(RectangleTemplateProperty); }
            set { SetValue(RectangleTemplateProperty, value); }
        }

        public static readonly DependencyProperty RectangleTemplateProperty =
            DependencyProperty.Register("RectangleTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));
        
        public DataTemplate RoundRectTemplate
        {
            get { return (DataTemplate)GetValue(RoundRectTemplateProperty); }
            set { SetValue(RoundRectTemplateProperty, value); }
        }

        public static readonly DependencyProperty RoundRectTemplateProperty =
            DependencyProperty.Register("RoundRectTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));
        
        public DataTemplate CircleTemplate
        {
            get { return (DataTemplate)GetValue(CircleTemplateProperty); }
            set { SetValue(CircleTemplateProperty, value); }
        }

        public static readonly DependencyProperty CircleTemplateProperty =
            DependencyProperty.Register("CircleTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));

        public DataTemplate CaretTemplate
        {
            get { return (DataTemplate)GetValue(CaretTemplateProperty); }
            set { SetValue(CaretTemplateProperty, value); }
        }

        public static readonly DependencyProperty CaretTemplateProperty =
            DependencyProperty.Register("CaretTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));

        public DataTemplate TrianglePeakUpTemplate
        {
            get { return (DataTemplate)GetValue(TrianglePeakUpTemplateProperty); }
            set { SetValue(TrianglePeakUpTemplateProperty, value); }
        }

        public static readonly DependencyProperty TrianglePeakUpTemplateProperty =
            DependencyProperty.Register("TrianglePeakUpTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));

        public DataTemplate TrianglePeakDownTemplate
        {
            get { return (DataTemplate)GetValue(TrianglePeakDownTemplateProperty); }
            set { SetValue(TrianglePeakDownTemplateProperty, value); }
        }

        public static readonly DependencyProperty TrianglePeakDownTemplateProperty =
            DependencyProperty.Register("TrianglePeakDownTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));

        public DataTemplate NoneTemplate
        {
            get { return (DataTemplate)GetValue(NoneTemplateProperty); }
            set { SetValue(NoneTemplateProperty, value); }
        }

        public static readonly DependencyProperty NoneTemplateProperty =
            DependencyProperty.Register("NoneTemplate", typeof(DataTemplate), typeof(NodeShapeToTemplateConverter), new PropertyMetadata(null));

        #endregion

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var node = value as INode;
            if (node == null)
            {
                return null;
            }
            DataTemplate template = null;
            switch (node.NodeShape)
            {
                case NodeShape.Rectangle:
                    template = RectangleTemplate;
                    break;
                case NodeShape.RoundRect:
                    template = RoundRectTemplate;
                    break;
                case NodeShape.Circle:
                    template = CircleTemplate;
                    break;
                case NodeShape.Caret:
                    template = CaretTemplate;
                    break;
                case NodeShape.TrianglePeakUp:
                    template = TrianglePeakUpTemplate;
                    break;
                case NodeShape.TrianglePeakDown:
                    template = TrianglePeakDownTemplate;
                    break;
                case NodeShape.None:
                    template = NoneTemplate;
                    break;
            }
            return template;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
